<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		changeConfigAttr,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';

	export default {
		data() {
			return buildFormData({
				userIdCache:'hr-visitorReception-entertainApply-userId-cache',
				partakeInstCache:'hr-visitorReception-entertainApply-partakeInst-cache',
				partakeUserCache:'hr-visitorReception-entertainApply-partakeUser-cache',
				feesAggregatedWayCache:'hr-visitorReception-entertainApply-feesAggregatedWay-cache',
				researchDevelopmentCache:'hr-visitorReception-entertainApply-researchDevelopment-cache',
				businessOpportunityCache:'hr-visitorReception-entertainApply-businessOpportunity-cache',
				marketingPlanningProductCache:'hr-visitorReception-entertainApply-marketingPlanningProduct-cache',
				saleOrderCache:'hr-visitorReception-entertainApply-saleOrder-cache',
				newProductCache:'hr-visitorReception-entertainApply-newProduct-cache',
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId', 'salaryPosId', 'salaryPosName', 'salaryInstId', 'salaryInstName'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		methods: {
			initLoad(options) {
				let _that = this;
				var fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code), //制表岗位
					{
						url:'dictSource/loadCombo',
						param:{
							sourceId:'289',
							categoryId:'8467'
						}
					}
				]
				if(fileId){
					ajaxArr.push({
						url:'commonForm/getData',
						param:{
							fileId:fileId,
							formCode: 'FIN1217'
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posName'), mainData);
					changeEditor('valueField', 'visitorCateId', 'data', configSelectData(res[1], 'id', 'text'), mainData);
					
					changeEditor("valueField","userId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								firstUserId: _that.userId
							}),
							url:'temployee/getStaffCombogrid',
							namefield:'userPosInstName',
							initload:true,
							searchfield:'q',
							cachename:_that.userIdCache
						}
					},null,mainData)
							
					changeEditor("valueField","partakeInstId",{
						"link":'../../public/search/search',
						"linkParam":{
							other:JSON.stringify(),
							url:'tinstitution/getComboTree',
							namefield:'text',
							initload:true,
							cachename:_that.partakeInstCache
						}
					},null,mainData)
							
					changeEditor("valueField","partakeUserId",{
						"link":'../../public/search/search',
						"linkParam":{
							other:JSON.stringify(),
							url:'orgInstPosUserOfficial/getUserListByInstPosId',
							namefield:'text',
							initload:true,
							cachename:_that.partakeUserCache
						}
					},null,mainData)
					
					changeEditor("valueField","feesAggregatedWayCode",{
						"linkParam":{
							other:JSON.stringify({
								sourceId: '289',
								categoryId: '8527'
							}),
							url:'dictSource/loadCombo',
							namefield:'text',
							initload:true,
							cachename:_that.feesAggregatedWayCache
						}
					},null,mainData)
					
					changeEditor("valueField","researchDevelopmentId",{
						"link":'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								firstId:''
							}),
							url:'createProjectShow/findCreateProjectShowPage',
							namefield:"projectName",
							notefield:"projectCode",
							extrafield:"typeName",
							searchfield:'q',
							initload:true,
							cachename:_that.researchDevelopmentCache
						}
					},null,mainData)
					
					changeEditor("valueField","businessOpportunityId",{
						"link":'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								firstId:''
							}),
							url:'biddingProject/getBidding',
							namefield:'biddingProjectName',
							notefield:'biddingCode',
							searchfield:'q',
							initload:true,
							cachename:_that.businessOpportunityCache
						}
					},null,mainData)
					
					changeEditor("valueField","marketingPlanningProductId",{
						"link":'../../public/search/search',
						"linkParam":{
							other:JSON.stringify({
								firstId:''
							}),
							url:'inventoryCategory/getExistingProductLineAssortment',
							namefield:'name',
							initload:true,
							cachename:_that.marketingPlanningProductCache
						}
					},null,mainData)
					
					changeEditor("valueField","saleOrderCode",{
						"link":'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								firstId:''
							}),
							url:'SaleOrderReviewShow/loadDataGrid',
							namefield:'customerNameX',
							notefield:'customerRequirementCode',
							searchfield:'q',
							initload:true,
							cachename:_that.saleOrderCache
						}
					},null,mainData)
					
					changeEditor("valueField","newProductId",{
						"link":'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								firstId:'',
								likeFlag:false
							}),
							url:'productShowSecond/getProductPage',
							namefield:'newProductName',
							notefield:'newProductCode',
							initload:true,
							cachename:_that.newProductCache
						}
					},null,mainData)
					
					let tempFileList = []
					let fileUrlList = []
					changeEditor('bottominfo','bottomFileUpload','fileUploadKey',"139",mainData)
					changeEditor("bottominfo","bottomFileUpload","tempFileList", tempFileList,mainData)
					changeEditor("bottominfo","bottomFileUpload","fileUrlList", fileUrlList,mainData)
					
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push('id')
						for(let index in res[2].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[2].value[index])
								_that.$set(_that.formdata,index,res[2].value[index])
							}
						}
						
						if(_that.formdata.bottomFileList){
							let tempFileList = []
							let fileUrlList = _that.formdata.bottomFileList.map(t=>{
								tempFileList.push(t.fileSaveName)
								return t.fileSaveName
							})
							changeEditor("bottominfo","bottomFileUpload","tempFileList", tempFileList,mainData)
							changeEditor("bottominfo","bottomFileUpload","fileUrlList", fileUrlList,mainData)
						}
						
						changeEditor("valueField","userId",{
							link:'../../public/pageSearch/pageSearch',
							"linkParam":{
								other:JSON.stringify({
									firstUserId:res[2].value.userId
								}),
								url:'temployee/getStaffCombogrid',
								namefield:'userPosInstName',
								initload:true,
								searchfield:'q',
								cachename:_that.userIdCache
							}
						},null,mainData)
						
						changeEditor("valueField","partakeUserId",{
							"link":'../../public/search/search',
							"linkParam":{
								other:JSON.stringify({
									instId:res[2].value.partakeInstId
								}),
								url:'orgInstPosUserOfficial/getUserListByInstPosId',
								initload:true,
								namefield:'username',
								cachename:_that.partakeUserCache
							}
						},null,mainData)
						
						if(_that.formdata.feesAggregatedWayCode){
							let idArr = _that.formdata.feesAggregatedWayCode.split(",")
							let nameArr = _that.formdata.feesAggregatedWayName.split(",")
							let feesAggregatedWayList = []
							for(let index in idArr){
								let obj = {
									id:idArr[index],
									text:nameArr[index]
								}
								feesAggregatedWayList.push(obj)
							}
							changeEditor('valueField', 'feesAggregatedWayCode', {
								initSelect: feesAggregatedWayList
							},null,mainData)
						}
					}
					
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						}
						_that.$refs.autoform.initSelectIndex("valueField","visitorCateId")
						
						if(!_that.$refs.autoform.formdata.userId){
							_that.$refs.autoform.formdata.userId = _that.userId
							_that.$refs.autoform.formdata.userName = _that.userName
							_that.$refs.autoform.formdata.posId = _that.salaryPosId
							_that.$refs.autoform.formdata.posName = _that.salaryPosName
							_that.$refs.autoform.formdata.instId = _that.salaryInstId
							_that.$refs.autoform.formdata.instName = _that.salaryInstName
						}
					})
				}, options)
			},
			formSubmit() {
				let _that = this
				
				_that.formdata.updateBottomFileFlag= true
				if(Object.prototype.toString.call(_that.$refs.autoform.formdata.bottomFileList) == "[object String]"){
					_that.formdata.bottomFileListStr = _that.$refs.autoform.formdata.bottomFileList|| ''
				}else{
					_that.formdata.bottomFileListStr = JSON.stringify(_that.$refs.autoform.formdata.bottomFileList || []) || ''
					
				}
				delete _that.formdata.bottomFileUpload
				delete _that.formdata.bottomFileList
				
				_that.om.formSave("entertainApply/save",_that.formdata,_that)
			},
			onSelectPartakeInst(id){
				let _that = this
				_that.$refs.autoform.changeEditor("valueField","partakeUserId",{
					"link":'../../public/search/search',
					"linkParam":{
						other:JSON.stringify({
							instId:id
						}),
						url:'orgInstPosUserOfficial/getUserListByInstPosId',
						initload:true,
						namefield:'username',
						cachename:_that.partakeUserCache
					}
				})
			},
			clearUserName(){
				this.$refs.autoform.clear("valueField","userId")
				this.$refs.autoform.clear("valueField","posId")
				this.$refs.autoform.clear("valueField","instId")
			},
			inputAccompanyNum(num){
				let _that = this
				if (num && !/(^[1-9]\d*$)/.test(num)) {
					_that.$refs.autoform.formdata.accompanyNum = Math.round(num)
				}
			},
			inputAccompanyUserNum(num){
				let _that = this
				if (num && !/(^[1-9]\d*$)/.test(num)) {
					_that.$refs.autoform.formdata.accompanyUserNum = Math.round(num)
				}
			},
			clearResearchDevelopment(){
				this.$refs.autoform.clear("valueField","researchDevelopmentId")
				this.$refs.autoform.clear("valueField","researchDevelopmentCode")
			},
			clearBusinessOpportunity(){
				this.$refs.autoform.clear("valueField","businessOpportunityId")
				this.$refs.autoform.clear("valueField","businessOpportunityCode")
			},
			clearPartakeInst(){
				this.$refs.autoform.clear("valueField","partakeInstId")
				this.$refs.autoform.clear("valueField","partakeUserId")
			},
			inputEstimatedAmount(num){
				if(!num){
					this.$refs.autoform.clear("valueField","estimatedAmount")
				}
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this;
			
			uni.getStorage({
				key:_that.userIdCache,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.userId = d.id
						formdata.userName =d.username
						formdata.posId = d.salaryPosId
						formdata.posName =d.salaryPosName
						formdata.instId = d.salaryPosInstId
						formdata.instName =d.salaryPosInstName
						uni.removeStorage({
							key:_that.userIdCache
						})
					}
				}
			});
			uni.getStorage({
				key: _that.partakeInstCache,
				success: function(res) {
					if(res.data){
						let d = res.data
						var formdata  = _that.$refs.autoform.formdata
						formdata.partakeInstId = d.id
						formdata.partakeInstName = d.text
						_that.onSelectPartakeInst(d.id)
						if(_that.$refs.autoform.formdata.partakeUserId){
							_that.$refs.autoform.clear("valueField","partakeUserId")
						}
						uni.removeStorage({
							key: _that.partakeInstCache
						});
					}
				}
			});
			uni.getStorage({
				key:_that.partakeUserCache,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.partakeUserId = d.id
						formdata.partakeUserName = d.username
					}
					uni.removeStorage({
						key:_that.partakeUserCache
					})
				}
			});
			uni.getStorage({
				key: _that.feesAggregatedWayCache, 
				success: function(res) {
					if(res.data){
						let d = res.data
						var formdata  = _that.$refs.autoform.formdata
						let ids =  d.map(e=> e.id).join(',')
						let names =  d.map(e=> e.text).join(',')
						formdata.feesAggregatedWayCode = ids
						formdata.feesAggregatedWayName = names
						_that.$refs.autoform.changeEditor('valueField', 'feesAggregatedWayCode', {
							initSelect: d
						})
						uni.removeStorage({
							key: _that.feesAggregatedWayCache
						});
					}
				}
			});
			uni.getStorage({
				key:_that.researchDevelopmentCache,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.researchDevelopmentId = d.id
						formdata.researchDevelopmentName = d.projectName
						formdata.researchDevelopmentCode = d.projectCode
					}
					uni.removeStorage({
						key:_that.researchDevelopmentCache
					})
				}
			});
			uni.getStorage({
				key:_that.businessOpportunityCache,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.businessOpportunityId = d.id
						formdata.businessOpportunityName = d.biddingProjectName
						formdata.businessOpportunityCode = d.biddingCode
					}
					uni.removeStorage({
						key:_that.businessOpportunityCache
					})
				}
			});
			uni.getStorage({
				key:_that.marketingPlanningProductCache,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.marketingPlanningProductId = d.id
						formdata.marketingPlanningProductName = d.name
					}
					uni.removeStorage({
						key:_that.marketingPlanningProductCache
					})
				}
			});
			uni.getStorage({
				key:_that.saleOrderCache,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.saleOrderCode = d.customerRequirementCode
					}
					uni.removeStorage({
						key:_that.saleOrderCache
					})
				}
			});
			uni.getStorage({
				key:_that.newProductCache,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.newProductId = d.id
						formdata.newProductCode = d.newProductCode
					}
					uni.removeStorage({
						key:_that.newProductCache
					})
				}
			})
		}
	}
</script>

<style>
</style>
       